<template>
  <div class="app-container" style="height: 100%">
    <div
      class="app-container-inner"
      style="display: flex; flex-direction: column; flex: 1; height: 100%"
      v-loading="loading"
      element-loading-text="加载中..."
    >
      <div class="btn_search">
        <div style="display: flex; gap: 5px">
          <HPlainButton @click="createBlank">创建空白模版</HPlainButton>
          <el-dropdown trigger="click">
            <HButton>
              批量操作
              <el-icon class="el-icon--right">
                <ArrowDown />
              </el-icon>
            </HButton>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleClick(1)">
                  <el-icon>
                    <CircleCheck />
                  </el-icon>
                  批量启用
                </el-dropdown-item>
                <el-dropdown-item @click="handleClick(2)">
                  <el-icon>
                    <CircleCheck />
                  </el-icon>
                  批量禁用
                </el-dropdown-item>
                <el-dropdown-item @click="handleClick(3)">
                  <el-icon>
                    <Delete />
                  </el-icon>
                  批量删除
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        <div>
          <el-input v-model="name" placeholder="模版名称" clearable @keyup.enter="getList" />
          <HButton type="primary" @click="getList">
            <el-icon>
              <Search />
            </el-icon>
            搜索
          </HButton>
        </div>
      </div>
      <div class="table">
        <el-table
          :border="true"
          stripe
          resizable
          :data="tableData"
          style="width: 100%; position: absolute"
          height="100%"
          @selection-change="selectAll"
        >
          <el-table-column type="selection" fixed="left" width="40" />
          <el-table-column
            show-overflow-tooltip
            prop="name"
            fixed="left"
            label="模版名称"
            width="200"
          />
          <el-table-column show-overflow-tooltip prop="size" label="纸张尺寸 (宽*高)" width="200">
            <template v-slot="item">
              <span>{{ item.row.width }}mm</span>*<span>{{ item.row.height }}mm</span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip prop="rowQty" label="单行数量" width="90" />
          <el-table-column show-overflow-tooltip prop="status" label="模版状态" width="90">
            <template v-slot="item">
              <HStatus v-if="!item.row.status">启用</HStatus>
              <HStatus v-else type="info">禁用</HStatus>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip prop="createEmp" label="创建人" />
          <el-table-column show-overflow-tooltip prop="createTime" label="创建时间" width="180" />
          <el-table-column show-overflow-tooltip prop="updateTime" label="更新时间" width="180" />
          <el-table-column show-overflow-tooltip fixed="right" label="操作" width="60">
            <template v-slot="item">
              <div class="btns">
                <HLink @click="goDetail(item.row.id)">详细</HLink>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="footer">
        <Pagination
          ref="paginDom"
          :list_total="total"
          @size-change="getList"
          @current-change="getList"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted, watch } from 'vue'
  import { useRouter, useRoute } from 'vue-router'
  import {
    printTemplateList,
    activeDeletePrintTemplate,
    deletePrintTemplate,
  } from '../../../../api/code'
  import Pagination from '../../../../components/Pagination/index.vue'
  import { ElMessage, ElMessageBox } from 'element-plus'
  import HPlainButton from '@/components/common/button/HPlainButton.vue'
  import HButton from '@/components/common/button/HButton.vue'
  import { ArrowDown, CircleCheck, Delete } from '@element-plus/icons-vue'
  import HStatus from '@/components/common/status/HStatus.vue'
  import HLink from '@/components/common/link/HLink.vue'
  import pageComponentName from '@/config/page-component-name'

  defineOptions({
    name: pageComponentName.snCode.printTemplate,
  })

  const router = useRouter()
  const route = useRoute()
  const total = ref(0)
  const name = ref(null)
  const loading = ref(true)
  const paginDom = ref() // 分页dom
  const deleteId = ref([])
  const createBlank = () => {
    router.push({ path: `/code/blank/${1}` })
  }
  const goDetail = (id) => {
    router.push({ path: `/code/blank/${id}` })
  }
  const tableData = ref(null)
  onMounted(() => {
    getList()
  })
  const getList = () => {
    loading.value = true
    let data = {
      limit: paginDom.value.size || 0,
      name: name.value || '',
      page: paginDom.value.current || 0,
    }
    printTemplateList(data).then((res) => {
      if (res.data.success) {
        tableData.value = res.data.data.rows
        total.value = parseInt(res.data.data.total) || 0
      } else {
        if (res.data.isError)
          ElMessage({
            showClose: true,
            message: res.data.message,
            type: 'error',
            duration: 4000,
          })
      }
      loading.value = false
    })
  }
  const handleClick = (val) => {
    if (deleteId.value.length == 0) {
      ElMessage({
        showClose: true,
        message: '请先选择操作对象！',
        type: 'warning',
        duration: 4000,
      })
      return
    }

    if (val == 3) {
      ElMessageBox.confirm('确定删除该模版吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        autofocus: false,
      }).then(() => {
        deletePrintTemplate(deleteId.value).then((res) => {
          if (res.data.success) {
            ElMessage({
              showClose: true,
              message: res.data.message,
              type: 'success',
              duration: 4000,
            })
            getList()
          } else {
            if (res.data.isError)
              ElMessage({
                showClose: true,
                message: res.data.message,
                type: 'error',
                duration: 4000,
              })
          }
        })
      })
      return
    }
    let status = false
    if (val == 2) {
      status = true
    }

    ElMessageBox.confirm(status ? '确定禁用该模版吗？' : '确定启用该模版吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      autofocus: false,
    }).then(() => {
      activeDeletePrintTemplate(deleteId.value, status).then((res) => {
        if (res.data.success) {
          ElMessage({
            showClose: true,
            message: res.data.message,
            type: 'success',
            duration: 4000,
          })
          getList()
        } else {
          if (res.data.isError)
            ElMessage({
              showClose: true,
              message: res.data.message,
              type: 'error',
              duration: 4000,
            })
        }
      })
    })
  }
  const selectAll = (val) => {
    deleteId.value = val.map((o) => o.id)
  }
  watch(route, (value) => {
    if (value.name === 'printTemp') {
      getList()
    }
  })
</script>

<style scoped lang="scss">
  @import './index';
</style>
